home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 710 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. Path: cs.mu.OZ.AU!bounce-back
  2. From: clamage@Eng.Sun.COM (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: String value of enum
  5. Date: 14 Mar 96 05:22:00 GMT
  6. Organization: Sun Microsystems Inc., Mountain View, CA
  7. Approved: fjh@cs.mu.oz.au
  8. Message-ID: <4i8a38$2qq@engnews1.Eng.Sun.COM>
  9. References: <4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com>
  10. NNTP-Posting-Host: munta.cs.mu.oz.au
  11. X-Original-Date: 14 Mar 1996 05:16:24 GMT
  12. Return-Path: <daemon@meeker.UCAR.EDU>
  13. X-Newsreader: NN version 6.5.0 #21 (NOV)
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBFAgUBMUetEOEDnX0m9pzZAQE11wF+LnWI9dP8nbJ90pXxXmBbqvU30n3CRQ58
  16.     DZTpRiDEjEHhxdgnTDqc3XUgwvqpG8gT
  17.     =iCQy
  18. Originator: fjh@munta.cs.mu.OZ.AU
  19.  
  20. mtm4@rsvl.unisys.com (Michael McCormick) writes:
  21.  
  22. >"W. Dicks" <wd@isis.co.za> shared the following on 13 Mar 96 07:31:13 GMT:
  23.  
  24. >>The system that I'm working often needs to know the string 
  25. >>value of an enum. e.g. enum week{MON=1, ..., SUN} it; it = 
  26. >>TUE;
  27. >>Then when this enum is passed as a parameter the function 
  28. >>should return the string based on the enum. For instance, 
  29. >>weekImage(it) will return "TUE". Is it not possible that such 
  30. >>a functionality can be built into enums?
  31.  
  32. >What you are essentially asking for is language support for converting
  33. >a label into a string respresentation of its name.  That is a very
  34. >unusual feature to find in any language, since it would require the
  35. >compiler to place the symbolic dictionary in the executable file.
  36.  
  37. It would not in fact be difficult to do, since only a table of
  38. strings per enum type would be needed. The information is normally
  39. put into debug info if you compile in debug mode anyway so the
  40. debugger can display the labels instead of the values. A language
  41. mechanism to make that data always available is probably too
  42. expensive a price for every program to pay in order to support
  43. this requirement which isn't very common and which can be solved
  44. easily in source code.
  45.  
  46.  
  47. >Since enum is by definition an integral type, why not assign values to your
  48. >enumerators that can be used as indexes into a string?:
  49.  
  50. > enum week {MON=0,TUE=4,WED=8,THU=12,FRI=16,SAT=20,SUN=24};
  51. > const char * days = "MON\0TUE\0WED\0THU\0FRI\0SAT\0SUN";
  52.  
  53. > char const * weekImage(week day) = days[day];
  54.  
  55. I would suggest instead this approach, which does not require you
  56. to count characters:
  57. const char* days[] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
  58. It "wastes" 7 extra pointers, but is easier to write and maintain.
  59.  
  60. The more general case of non-contiguous enum values could be handled
  61. by a table of value/string pairs and a lookup function. The standard
  62. "map" class will do it all for you.
  63. --
  64. Steve Clamage, stephen.clamage@eng.sun.com
  65. ---
  66. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  67. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  68. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  69. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  70. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  71.